<?php

namespace App\Http\Controllers\Commit;

use App\Repository\Commit\RoleRepository;
use  App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
use App\Repository\MenuRepository;
use Illuminate\Support\Facades\DB;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class RoleController extends BaseController
{
  public function __construct(RoleRepository $DictDatarRepository, MenuRepository $MenuRepository)
  {
    $this->repoist = $DictDatarRepository;
    $this->menuModel = $MenuRepository;
  }
  public function index(Request $Request)
  {
    $where = [];
    if (!empty($Request->get('status'))) {
      $where['where']['status'] = $Request->get('status');
    }
    if (!empty($Request->get('roleName'))) {
      $where['like'] = ['role_name' => $Request->get('roleName')];
    }
    if (!empty($Request->get('roleKey'))) {
      $where['where']['role_key'] = $Request->get('roleKey');
    }
    $reque = $Request->all();
    if (!empty($reque['params']['beginTime'])) {
      $end_time = date('Y-m-d H:i:s');
      if (!empty($reque['params']['endTime'])) {
        $end_time = $reque['params']['endTime'];
      }
      $where['between'] = ['create_time', [$reque['params']['beginTime'], $end_time]];
    }
    if ($Request->user->role_id != 1) {
      $where['where']['shop_id'] = $Request->user->shop_id;
    }

    // var_dump($where,1111111111);
    $data =  $this->repoist->index($where, $Request->get('pageNum'), $Request->get('pageSize'));
    return $data;
  }
  public function store(Request $request)
  {
    $request->offsetSet('role_name', $request->get('roleName'));
    $request->offsetSet('role_key', $request->get('roleKey'));
    $request->offsetSet('role_sort', $request->get('roleSort'));
    $request->offsetSet('data_scope', $request->get('dataScope'));
    $request->offsetSet('menu_check_strictly', $request->get('menuCheckStrictly'));
    $request->offsetSet('dept_check_strictly', $request->get('deptCheckStrictly'));
    $request->offsetSet('del_flag', $request->get('delFlag'));
    if (!empty($request->get('menuIds'))) {
      $request->offsetSet('in_ids', implode(',', $request->get('menuIds')));
    }
    return $this->repoist->add($request);
  }
  public function save(Request $request)
  {
    if (!$request->get('roleId')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['role_id' => $request->get('roleId')];
    $request->offsetSet('role_id', $request->get('roleId'));
    $request->offsetSet('role_name', $request->get('roleName'));
    $request->offsetSet('role_key', $request->get('roleKey'));
    $request->offsetSet('role_sort', $request->get('roleSort'));
    $request->offsetSet('data_scope', $request->get('dataScope'));
    $request->offsetSet('menu_check_strictly', $request->get('menuCheckStrictly'));
    $request->offsetSet('dept_check_strictly', $request->get('deptCheckStrictly'));
    $request->offsetSet('del_flag', $request->get('delFlag'));
    if (!empty($request->get('menuIds'))) {
      $request->offsetSet('in_ids', implode(',', $request->get('menuIds')));
    }
    return $this->repoist->edit($where, $request);
  }
  /**
   * @OA\Get(
   *     path="/api/v1/menu/id",
   *     tags={"后台菜单模块"},
   *     summary="单条数据显示",
   *     @OA\Parameter(name="token", in="header", @OA\Schema(type="string")),
   *     @OA\Response(response=200, description="  {code: int, msg:string, data:[token=>1]}  "
   *     )
   * )
   */
  /**
   * 查询单条数据
   *
   * @param Request $id
   */
  public function  show(Request $request, $id)
  {
    $where = ['role_id' => $id];
    if ($request->user->role_id != 1) {
      $where = ['role_id' => $id, 'shop_id' => $request->user->shop_id];
    }

    //查询一条数据
    $return = $this->repoist->getOne($where);
    if (!empty($return)) {
      if (!empty($return['menu_check_strictly'])) {
        $return['menu_check_strictly'] = true;
      }
      if (!empty($return['dept_check_strictly'])) {
        $return['dept_check_strictly'] = true;
      }
      $return = usfirstArr($return);
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'role_id' => $id,
      'shop_id' => $request->user->shop_id,
    ];
    return $this->repoist->del($where);
  }
  // 权限 没选中的权限
  public function excludeIndex(Request $request, $id)
  {
    // $where[] = ['ancestors','like','%.'.$id.'.%'];
    $where = ['role_id' => $id];
    $data = $this->repoist->getOne($where);
    // var_dump($data);
    $role_id = $request->user->role_id;
    if (empty($role_id)) {
      $wheres = [
        'dept_id' => $request->user->dept_id,
      ];
      $res = DB::table('dept')->where($wheres)->first();
      if (!$res) {
        return $this->returnMsg(100, '未查询到您的权限');
      }
      $role_id = $res->role_id;
    }
    return [
      'code' => 200,
      'msg'  => '查询成功',
      'checkedKeys' => explode(',', $data['in_ids']),
      'menus' => $this->menuModel->getOneTrue([], [], 0, 5, $role_id)
    ];
  }
}
